package com.hcrazy.manchester.app.controllers.coupon;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.User;
import com.hcrazy.manchester.app.services.BookMenuService;
import com.hcrazy.manchester.app.services.CouponSellerService;
import com.hcrazy.manchester.app.services.CouponService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/api/coupon")
public class CouponAPIController {

	@Autowired
	private CouponService couponService;
	
	@Autowired
	private BookMenuService sortService;
	
	@Autowired
	private CouponSellerService sellerService;
	
	@RequestMapping(value="/sorts",produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getAllBookMenus(){
		try {
			return sortService.getAllMenus(Constant.TYPE_COUPON, 0);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/sellers",produces=MediaType.APPLICATION_JSON_VALUE,method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody Object getAllSellers(
			@RequestParam(required=false) String search,
			@RequestParam(required=false) Integer sortId,
			@RequestParam(required=false) Integer pageNum,
			@RequestParam(required=false) Integer numPerPage){
		try {
			if(null == search)
				search = "";
			if(null == sortId)
				sortId = 0;
			if(null == pageNum)
				pageNum = 1;
			if(null == numPerPage)
				numPerPage = 10;
			
			PageModel pageModel = getAllCouponSellers(pageNum,numPerPage,sortId,search);
			return pageModel;
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/seller/addresses",produces=MediaType.APPLICATION_JSON_VALUE,method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody Object getAllCouponAddrsses(@RequestParam int sellerId){
		try {
			return sellerService.getAddressesBySellerId(sellerId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/coupons",produces=MediaType.APPLICATION_JSON_VALUE,method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody Object getAllCoupons(
			@RequestParam(required=false) String name,
			@RequestParam(required=false) Integer sortId,
			@RequestParam(required=false) Integer sellerId,
			@RequestParam(required=false) Integer pageNum,
			@RequestParam(required=false) Integer numPerPage){
		try {
			if(null == name)
				name = "";
			if(null == sortId)
				sortId = 0;
			if(null == sellerId)
				sellerId = 0;
			if(null == pageNum)
				pageNum = 1;
			if(null == numPerPage)
				numPerPage = 10;
			
			PageModel pageModel = getAllCoupons(pageNum, numPerPage, sortId, name, sellerId,0);
			
			return pageModel;
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/bag/coupons",produces=MediaType.APPLICATION_JSON_VALUE,method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody Object getBagCoupons(
			HttpServletRequest req,
			@RequestParam(required=false) String name,
			@RequestParam(required=false) Integer sortId,
			@RequestParam(required=false) Integer sellerId,
			@RequestParam(required=false) Integer pageNum,
			@RequestParam(required=false) Integer numPerPage){
		try {
			
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录！");
			}
			
			User user = (User)obj;
			long userId = user.getUserId();
			
			if(null == name)
				name = "";
			if(null == sortId)
				sortId = 0;
			if(null == sellerId)
				sellerId = 0;
			if(null == pageNum)
				pageNum = 1;
			if(null == numPerPage)
				numPerPage = 10;
			
			PageModel pageModel = getAllCoupons(pageNum, numPerPage, sortId, name, sellerId,userId);
			
			return pageModel;
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	/**
	 * 领取优惠券
	 * @return
	 */
	@RequestMapping(value="/receive",method={RequestMethod.POST,RequestMethod.GET},produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object receiveCoupon(
			HttpServletRequest req,
			@RequestParam int couponId,
			@RequestParam(required=false) String password){
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不允许领取优惠券！");
			}
			
			User user = (User)obj;
			long userId = user.getUserId();
			
			return couponService.addUserCoupon(userId, couponId,password);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	/**
	 * 领取优惠券
	 * @return
	 */
	@RequestMapping(value="/use",method={RequestMethod.POST,RequestMethod.GET},produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object useCoupon(
			HttpServletRequest req,
			@RequestParam int couponId){
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不允许使用优惠券！");
			}
			
			User user = (User)obj;
			long userId = user.getUserId();
			
			couponService.updateCouponStatus(userId, couponId, Constant.COUPON_STATUS_USE);
			
			Map<String,String> result = new HashMap<String,String>();
			result.put(Constant.SINGLE_RETURN,"true");
			
			return result;
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	
	/**
	 * 查询全部信息列表
	 * @param pageNum
	 * @param sortId
	 * @param search
	 * @return PageModel
	 */
	private PageModel getAllCouponSellers(int pageNum,int numPerPage,int sortId,String search){
		PageModel model = new PageModel();
		model.setNumPerPage(numPerPage);
		model.setPageNum(pageNum);
		try {
			return sellerService.getCouponSellers(model, search, sortId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 查询全部信息列表
	 * @param pageNum
	 * @param sortId
	 * @param search
	 * @return PageModel
	 */
	private PageModel getAllCoupons(int pageNum,int numPerPage,int sortId,String name,int sellerId,long userId){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);
		try {
			if(userId > 0){
				return couponService.getAllCoupons(model, name, sortId,sellerId,userId);
			}else{
				return couponService.getAllCoupons(model, name, sortId,sellerId);
			}
			
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
}
